//
//  YTDriverAuthLicenseViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/12.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTDriverAuthLicenseViewController.h"
#import "YTDriverAuthLicenseViewModel.h"
#import "YTPickerView.h"

@interface YTDriverAuthLicenseViewController ()
@property (weak, nonatomic) IBOutlet UITextField *licenseNumText;
@property (weak, nonatomic) IBOutlet UIButton *licenseFaceImage;
@property (weak, nonatomic) IBOutlet UILabel *startText;
@property (weak, nonatomic) IBOutlet UILabel *endText;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (strong, nonatomic) YTDriverAuthLicenseViewModel *viewModel;
@property (strong, nonatomic) UIImage *faceImage;
@end

@implementation YTDriverAuthLicenseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)dealloc {}

- (void)initialize {
    _viewModel = [[YTDriverAuthLicenseViewModel alloc] init];
}

- (void)bindViewModel {
    @weakify(self);
    [[_licenseFaceImage rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *button) {
        @strongify(self);
        // 选择照片
        [self yt_selecteImageCompletion:^(UIImage * _Nullable image) {
            [button setBackgroundImage:image forState:UIControlStateNormal];
            self.faceImage = image;
        }];
    }];
    
    RAC(_viewModel, licenseNum) = _licenseNumText.rac_textSignal;
    RAC(_viewModel, startDate) = RACObserve(_startText, text);
    RAC(_viewModel, endDate) = RACObserve(_endText, text);
    RAC(_viewModel, faceImage) = RACObserve(self, faceImage);
    
    _submitBtn.rac_command = _viewModel.submitCommand;
    
    [[_viewModel.submitCommand.executionSignals switchToLatest] subscribeNext:^(NSDictionary *resonse) {
        @strongify(self);
        if ([resonse[@"resultCode"] integerValue] == 1000) {
            [self performSegueWithIdentifier:@"YTDriverAuthWait" sender:nil];
        }
    }];
    
    [_viewModel.submitCommand.enabled subscribeNext:^(NSNumber *enabled) {
        @strongify(self);
        self.submitBtn.backgroundColor = enabled.boolValue ? kMainBlueColor : kGrayColor;
    }];
    
    [_viewModel.submitCommand.executing subscribeNext:[self yt_showHudWithTips:@"拼命上传中..."]];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.view endEditing:YES];
    YTPickerView *picker = [YTPickerView pickerWithType:YTPickerTypeMonthDay];
    picker.birthday = YES;
    
    if (indexPath.row == 1) {
        picker.tips = @"领取驾照日期";
        picker.completion = ^(NSString *selectedString) {
            _startText.textColor = kBlackColor;
            _startText.text = selectedString;
        };
    }
    
    if (indexPath.row == 2) {
        picker.tips = @"证件有效期至";
        picker.completion = ^(NSString *selectedString) {
            _endText.textColor = kBlackColor;
            _endText.text = selectedString;
        };
    }
    
    [picker show];
}

@end
